const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cors = require('cors')
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser');
const logger = require('morgan');

const app = express();
process.env.PORT = "8888"
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors())
//role
const indexRouter = require('./routes/menus');
const usersRouter = require('./routes/user');
const loginRouter = require('./routes/login');
const roleListRouter = require('./routes/role/roleList');
const changeRoleRouter = require('./routes/role/changeRole');
const treeListRouter = require('./routes/role/treeList');
const getRoleInfoRouter = require('./routes/role/getRoleInfo');
const addRoleRouter = require('./routes/role/addRole');
const updateRoleRouter = require('./routes/role/updateRole');
const deleteRole = require('./routes/role/deleteRole');
//menu
const menuListRouter = require('./routes/menu/menuList');
const menuTreeListRouter = require('./routes/menu/menuTreeList');
const getMenuInfoRouter = require('./routes/menu/getMenuInfo');
const addMenuRouter = require('./routes/menu/addMenu');
const updateMenuRouter = require('./routes/menu/updateMenu');
const deleteMenu = require('./routes/menu/deleteMenu');
//user
const userGetTree = require('./routes/user/userGetTree');
const userList = require('./routes/user/userList');
const deleteUser = require('./routes/user/deleteUser');
const changeUser = require('./routes/user/changeUser');
const departmentTreeList = require('./routes/user/departmentTreeList');
const getRoleList = require('./routes/user/getRoleList');
const addUser = require('./routes/user/addUser');
const updateUser = require('./routes/user/updateUser');
const getUserInfo = require('./routes/user/getUserInfo');

//department
const departmentList = require('./routes/department/departmentList');
const addDepartment = require('./routes/department/addDepartment');
const updateDepartment = require('./routes/department/updateDepartment');
const getDepartmentInfo = require('./routes/department/getDepartmentInfo');
const deleteDepartment = require('./routes/department/deleteDepartment');

//permission
const permissionList = require('./routes/permission/permissionList');
const addPermission = require('./routes/permission/addPermission');
const updatePermission = require('./routes/permission/updatePermission');
const exportFile = require('./routes/exportFile')

//bigScreen
const leftPageData = require('./routes/bigScreen/leftPageData');
const centerPageData = require('./routes/bigScreen/centerPageData');
const rightPageData = require('./routes/bigScreen/rightPageData');

app.use('/exportFile', exportFile)
//role
app.use('/menus', indexRouter);
app.use('/user', usersRouter);
app.use('/login', loginRouter);
app.use('/roleList', roleListRouter);
app.use('/treeList', treeListRouter);
app.use('/changeRole', changeRoleRouter);
app.use('/getRoleInfo', getRoleInfoRouter);
app.use('/addRole', addRoleRouter);
app.use('/updateRole', updateRoleRouter);
app.use('/deleteRole', deleteRole);

//menu
app.use('/menuList', menuListRouter);
app.use('/menuTreeList', menuTreeListRouter);
app.use('/getMenuInfo', getMenuInfoRouter);
app.use('/addMenu', addMenuRouter);
app.use('/updateMenu', updateMenuRouter);
app.use('/deleteMenu', deleteMenu);

//user
app.use('/userGetTree', userGetTree);
app.use('/userList', userList);
app.use('/deleteUser', deleteUser);
app.use('/changeUser', changeUser);
app.use('/departmentTreeList', departmentTreeList);
app.use('/getRoleList', getRoleList);
app.use('/addUser', addUser);
app.use('/updateUser', updateUser);
app.use('/getUserInfo', getUserInfo);

//department
app.use('/departmentList', departmentList);
app.use('/addDepartment', addDepartment);
app.use('/updateDepartment', updateDepartment);
app.use('/getDepartmentInfo', getDepartmentInfo);
app.use('/deleteDepartment', deleteDepartment);

//permission
app.use('/permissionList', permissionList);
app.use('/addPermission', addPermission);
app.use('/updatePermission', updatePermission);

//bigScreen
app.use('/leftPageData', leftPageData);
app.use('/centerPageData', centerPageData);
app.use('/rightPageData', rightPageData);

app.use(function (req, res, next) {
    next(createError(404));
});
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())

app.use(function (err, req, res, next) {
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
    res.status(err.status || 500);
    res.render('error');
});

module.exports = app;
